<template>
  <div>
    <el-form
      :model="postForm"
      ref="postForm"
      :rules="rules"
      label-width="120px"
      label-suffix=":"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          保存
        </el-button>
      </sticky>

      <div style="padding: 15px">
        <el-row>
          <el-col :span="22">
            <el-form-item label="商城名称" style="width: 300px">
              <el-input v-model.trim="postForm.app_name"/>
            </el-form-item>
            <el-form-item label="创建时间">
              <el-date-picker v-model.trim="postForm.create_time" type="date" />
            </el-form-item>
            <el-form-item label="域名" style="width: 500px">
              <el-input v-model.trim="postForm.domain"/>
            </el-form-item>
            <el-form-item label="商家码" style="width: 500px" v-if="false">
              <el-input v-model.trim="postForm.merchant" disabled />
            </el-form-item>
            <el-form-item label="推广码" style="width: 500px">
              <el-input v-model.trim="postForm.referrer" disabled/>
            </el-form-item>
            <el-form-item label="是否显示版本" style="width: 200px">
              <el-switch v-model="postForm.show_system" />
            </el-form-item>
            <el-form-item label="是否开启内测" style="width: 200px">
              <el-switch v-model="postForm.open_test" />
            </el-form-item>
            <el-form-item label="仅线下支付" style="width: 200px">
              <el-switch v-model="postForm.only_offline_pay" />
            </el-form-item>
            <el-form-item label="开启评论" style="width: 200px">
              <el-switch v-model="postForm.open_comment" />
            </el-form-item>
            <el-form-item label="是否开微信登录" style="width: 200px" v-if="false">
              <el-switch v-model="postForm.open_wx_login" />
            </el-form-item>
            <el-form-item label="线下收款码">
              <upload-image
                bucket="money"
                :max="1"
                :max-width="375"
                :quality="50"
                @onSuccess="onSuccess"
                @onRemove="onRemove"
                :file-list="postForm.money_qrcode"
              />
              <span>请上传微信收款码</span>
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import UploadImage from '@/components/Upload/UploadImage'
import Sticky from '@/components/Sticky'
import api from '@/api'
export default {
  name: 'SystemApp',
  components: {
    Sticky,
    UploadImage
  },
  data() {
    return {
      postForm: {
        status: 'draft'
      },
      rules: {
      },
      loading: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const { data } = await api.system.fetchApp()
      this.postForm = Object.assign({}, data)
      this.postForm.status = 'publisher'
      this.postForm.show_system = +this.postForm.show_system === 1
      this.postForm.open_test = +this.postForm.open_test === 1
      this.postForm.open_wx_login = +this.postForm.open_wx_login === 1
      this.postForm.only_offline_pay = +this.postForm.only_offline_pay === 1
      this.postForm.open_comment = +this.postForm.open_comment === 1
      // 图片
      if (this.postForm.money_qrcode) {
        const list = []
        list.push({
          id: +this.postForm.money_qrcode.id,
          // 这个没有缩略图
          url: this.postForm.money_qrcode.path
        })
        // this.postForm.money_qrcode = JSON.parse(JSON.stringify(list))
        this.postForm.money_qrcode = list
      } else {
        this.postForm.money_qrcode = []
      }
    },
    submitForm() {
      this.$refs.postForm.validate(validate => {
        if (validate) {
          this.loading = true
          // 图片
          let image = [0]
          if (this.postForm.money_qrcode && this.postForm.money_qrcode.length > 0) {
            image = this.postForm.money_qrcode.map(x => x.id)
          }
          // console.log(image)
          api.system.saveApp({
            ...this.postForm,
            show_system: this.postForm.show_system ? 1 : 0,
            open_test: this.postForm.open_test ? 1 : 0,
            open_wx_login: this.postForm.open_wx_login ? 1 : 0,
            only_offline_pay: this.postForm.only_offline_pay ? 1 : 0,
            open_comment: this.postForm.open_comment ? 1 : 0,
            money_qrcode: image[0]
          }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '保存成功',
              type: 'success',
              duration: 2000
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    /**
     * 图片上传
     */
    onSuccess(data, fileList) {
      if (data && data.id) {
        this.postForm.money_qrcode.push({
          id: data.id,
          url: data.full_name
        })
        // this.submitForm()
      }
    },
    /**
     * 删除图片
     */
    onRemove(data, fileList) {
      if (data && data.id) {
        this.postForm.money_qrcode = this.postForm.money_qrcode.filter(x => x.id !== data.id)
        this.submitForm()
      }
    }
  }
}
</script>
